﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.HVAC;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class LayPPipeAccessories : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Result result;
			try
			{
				if (commandData == null)
				{
					throw new System.ArgumentNullException();
				}
				UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
				Document document = activeUIDocument.Document;
				Selection selection = activeUIDocument.Selection;
				if (!YJKRevitTools.ViewCheck(activeUIDocument, new List<ViewType>
				{
					(ViewType)1,
					(ViewType)2,
					(ViewType)4,
					(ViewType)3,
					(ViewType)117
				}))
				{
					result = Autodesk.Revit.UI.Result.Cancelled;
				}
				else
				{
					FamilySymbol familySymbol = new ILayForm(document).LayAffordPPipeAccessoryFormShow(document);
					if (familySymbol == null)
					{
						result = Autodesk.Revit.UI.Result.Cancelled;
					}
					else
					{
						if (familySymbol != null)
						{
							activeUIDocument.PromptForFamilyInstancePlacement(familySymbol);
						}
						result = Autodesk.Revit.UI.Result.Succeeded;
					}
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				message = ex.Message;
				result = Autodesk.Revit.UI.Result.Failed;
			}
			return result;
		}
	}
}
